package com.totoro.transfer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.time.LocalTime;
import java.util.*;

public class T375 {

    public static void transfer() {

        //String dir = "/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/b57159745b91a3500b5571651c6ad49f/File";
        //String dir = "/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/f15e8c4552cc942be54d7c03940014b1/File";
        String dir = "/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/3ae126ce7089bf77172056a246750e3a/File";
        //图片
        Map<String, String> cache = new HashMap();
        try {
            BufferedReader readerCvs = new BufferedReader(new FileReader(new File(dir + "/data.csv")));
            String line1 = null;
            while ((line1 = readerCvs.readLine()) != null) {
                String key = line1.replaceAll("^.+/([^/]+)$", "$1");
                cache.put(key, line1);
                //cache.put(line1.split(",")[0], line1.split(",")[1]);
            }
            readerCvs.close();


            //File dirFile = new File("/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/b57159745b91a3500b5571651c6ad49f/File/srt格式/");
            File dirFile = new File("/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/733576650f039db5d1f7f4c73540e4ac/File/srt/");
            //File dirFile = new File("/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/f15e8c4552cc942be54d7c03940014b1/File/50-80/50/");
            File[] fileList = dirFile.listFiles();
            System.out.println(String.format("开始处理文件..."));

            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/Users/swy/Documents/transfer/" + "/sql100.sql")));

            for (int i = 0; i < fileList.length; i++) {
                if(fileList[i].getName().contains(".")){}

                List<String> list = new ArrayList();

                //region json处理
                //读取文件
                StringBuffer sb = new StringBuffer();
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(fileList[i]));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                        list.add(line);
                    }
                    reader.close();
                    //System.out.println("filename" + fileList[i].getName());
                    //System.out.println("filecontent" + sb.toString());
                } catch (Exception ex) {

                }

                //写入json
                JSONObject result = new JSONObject();
                JSONArray rects = new JSONArray();
                result.put("name", fileList[i].getName().replace("-字幕.srt", ""));
                LocalTime seconds = LocalTime.parse(list.get(list.size() - 3).split("-->")[1].trim().replace(",", "."));


                result.put("seconds", (seconds.getHour()*60*60) + (seconds.getMinute()*60) + seconds.getSecond() + seconds.getNano() / 1000000000.000f);

                File dirJson = new File("/Users/swy/Documents/transfer/", "json");
                if (!dirJson.exists()) {
                    dirJson.mkdir();
                }
                File jsonFile = new File(dirJson, fileList[i].getName().replace(".mp3", "").replace("-字幕.srt", "") + ".json");
                try {
                    //BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFile));
                    if (list.size() % 4 != 0) {
                        throw new Exception();
                    }
                    for (int k = 0; k < list.size(); k = k + 4) {
                        JSONObject temp = new JSONObject();
                        LocalTime start_seconds = LocalTime.parse(list.get(k + 1).split("-->")[0].trim().replace(",", "."));
                        float start = (start_seconds.getHour()*60*60) + (start_seconds.getMinute()*60) + start_seconds.getSecond() + (start_seconds.getNano() / 1000000000.000f);
                        temp.put("start_seconds", start);
                        //System.out.println("start_seconds:" + start_seconds.toString() + "---" + (start_seconds.getSecond() + (start_seconds.getNano() / 1000000000.000f)));

                        LocalTime end_seconds = LocalTime.parse(list.get(k + 1).split("-->")[1].trim().replace(",", "."));
                        float end = (end_seconds.getHour()*60*60) + (end_seconds.getMinute()*60) + end_seconds.getSecond() + (end_seconds.getNano() / 1000000000.000f);
                        temp.put("end_seconds", end);
                        //System.out.println("end_seconds:" + end_seconds.toString() + "---" + (end_seconds.getSecond() + (end_seconds.getNano() / 1000000000.000f)));

                        temp.put("x1", (start) * 70 + 20.5);
                        temp.put("x2", (end) * 70 + 20.5);

                        temp.put("y1", 0);
                        temp.put("y2", 200);
                        temp.put("id", System.currentTimeMillis() + k);
                        temp.put("noise", "");
                        temp.put("sex", "");
                        temp.put("identity", "");


                        temp.put("text", list.get(k + 2));
                        rects.add(temp);
                    }
                    result.put("rects", rects);
                    //writer.write(result.toJSONString());
                    //writer.flush();
                    //writer.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                //endregion

                //region SQL生成
                System.out.println(String.format(
                        "%s VALUES (414,'20180403051941','{\"image\":\"%s\"}',1,'{}',0,0,1,0,0,0,'{\"markData\":%s}',null);\n",
                        "INSERT INTO `tags_task_libs` (`tid`, `batch_id`, `question`, `type`, `properties`, `ctime`, `uid`, `status`, `lock`, `lock_status`, `lock_time`, `answer`, `log_id`)",
                        cache.get(fileList[i].getName().replace("-字幕.srt", "")),
                        result.toJSONString()
                ));
                writer.write(String.format(
                        "%s VALUES (414,'20180403051941','{\"image\":\"%s\"}',1,'{}',0,0,1,0,0,0,'{\"markData\":%s}',null);\n",
                        "INSERT INTO `tags_task_libs` (`tid`, `batch_id`, `question`, `type`, `properties`, `ctime`, `uid`, `status`, `lock`, `lock_status`, `lock_time`, `answer`, `log_id`)",
                        cache.get(fileList[i].getName().replace("-字幕.srt", "")),
                        result.toJSONString()
                ));

                //endregion
            }
            writer.flush();
            writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {
        transfer();
    }
}
